//
//  ZhiWeiGuanLiListVC.m
//  EnterpriseEdition
//
//  Created by ypj on 2021/10/10.
//

#import "ZhiWeiGuanLiListVC.h"
#import "ZhiWeiListCell.h"
#import "XinZengZhiWeiVC.h"
#import "ZhiWeiDetailPageVC.h"
#import "ZhiWeiGuanLiModel.h"
#import "SearchAndShaiXuanView.h"
#import "ZhiWeiGuanLiListTableViewCell.h"
#import "FuZeRenModel.h"
@interface ZhiWeiGuanLiListVC ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (nonatomic, strong) UITableView             *tableView;
@property (nonatomic, strong) NSMutableArray          *dataAry;
@property (nonatomic, assign) NSInteger               pageIndex;
@property (nonatomic, assign) NSInteger               totalPage;
@property (nonatomic, strong) FuZeRenModel            *selectModel;
@property (nonatomic, strong) SearchAndShaiXuanView   *shaiXuanView;
@property (nonatomic, strong) NSString                *jobName;//职位名称
@property (nonatomic, strong) NSString                *userName;//负责人名称
@end

@implementation ZhiWeiGuanLiListVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     
     self.shaiXuanView.phoneTF.text = @"";
     self.jobName = @"";
//    if (self.userName.length>0) {
//        self.shaiXuanView.zeRenRenLb.text=self.userName;
//    } else {
//        self.shaiXuanView.zeRenRenLb.text=@"按负责人筛选";
//    }
     self.pageIndex = 1;
     [self getZhiWeiList:NO];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
     self.userName = @"";
}
- (void)viewDidLoad {
     [super viewDidLoad];
     self.view.backgroundColor=White_Color;
    [self createTableView];
    [self dataAry];
}
-(void)createTableView{
     UIView *bgView = [[UIView alloc] init];
    [self.view addSubview:bgView];
        
    self.shaiXuanView = [[SearchAndShaiXuanView alloc] init];
    [bgView addSubview:self.shaiXuanView];
        
     self.tableView = [[UITableView alloc] init];
    [self.view addSubview:self.tableView];

     self.shaiXuanView.phoneTF.placeholder = @"请输入职位名称";
    [self.shaiXuanView.searchButton whenTapped:^{
        [self.view endEditing:YES];
         self.pageIndex = 1;
         self.jobName = self.shaiXuanView.phoneTF.text;
        [self getZhiWeiList:NO];
    }];
  

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
   
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:[ZhiWeiListCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[ZhiWeiListCell reuseIdentifier]];
//  [self.tableView registerClass:[ZhiWeiGuanLiListTableViewCell class] forCellReuseIdentifier:[ZhiWeiGuanLiListTableViewCell reuseIdentifier]];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self getZhiWeiList:NO];
    }];
    header.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = header;
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
         self.pageIndex++;
        if (self.pageIndex > self.totalPage){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self getZhiWeiList:YES];
        }
    }];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(ScreenWidth, 90));
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.top.equalTo(self.view.mas_top).offset(0);
    }];
    
    [_shaiXuanView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(ScreenWidth, 90));
        make.left.equalTo(bgView.mas_left).offset(0);
        make.right.equalTo(bgView.mas_right).offset(0);
        make.top.equalTo(bgView.mas_top).offset(0);

    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(ScreenWidth, B_HEIGHT-LL_StatusBarAndNavigationBarHeight-90-50));
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.top.equalTo(self.view.mas_top).offset(90);
    }];
}
#pragma mark - ------------------ DZNEmptyData ------------------
-(UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"No_order_icon"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"暂无数据";
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:15.0f],
                                 NSForegroundColorAttributeName:gray666_color
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    if (!self.tableView.mj_header.isRefreshing && !self.tableView.mj_footer.isRefreshing) {
        [self.tableView.mj_footer resetNoMoreData];
        self.pageIndex = 1;
       [self getZhiWeiList:NO];
    }
}
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -50;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataAry.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZhiWeiListCell *cell = [tableView dequeueReusableCellWithIdentifier:[ZhiWeiListCell reuseIdentifier]];
    if (!cell) {
        cell = [[ZhiWeiListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[ZhiWeiListCell reuseIdentifier]];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ZhiWeiGuanLiModel *model = self.dataAry[indexPath.row];
    cell.xinZiLb.text = model.salaryRange;
    cell.shuXingLb.text = model.driverNature;
    cell.paiZhaoLb.text = model.jobName;
    if ([model.jobStatus isEqualToString:@"1"]){
        cell.finishButton.hidden = NO;
        cell.rightWidth.constant = 87;
        cell.statusLb.text = @"已发布";
    } else if ([model.jobStatus isEqualToString:@"2"]){
        cell.finishButton.hidden = YES;
        cell.rightWidth.constant = 13;
        cell.statusLb.text = @"已完成";
    }
    [cell.delectButton whenTapped:^{
        //删除数据
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"确定删除该职位吗?" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
            [self selectInfo:model.ID];
        }]];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:nil];
    }];
    [cell.finishButton whenTapped:^{
        //已完成
        [self jobOkInfo:model.ID];
    }];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return [ZhiWeiListCell cellHeight];
   
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZhiWeiGuanLiModel *model=self.dataAry[indexPath.row];
    ZhiWeiDetailPageVC *pageVC =[[ZhiWeiDetailPageVC alloc] init];
    pageVC.model = model;
    [self pushToNextController:pageVC];

}
#pragma mark ----------------<#note#>----------------
- (NSMutableArray *)dataAry {
    if (!_dataAry) {
        self.dataAry = [@[] mutableCopy];
    }
    return _dataAry;
}

///删除职位
-(void)selectInfo:(NSString *)ID{
    NSString *url = [NSString stringWithFormat:@"%@?id=%@",Del_Job_Url,ID];
    [MSNetworkManager postRevertUrl:url params:@{} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        if (isSuccess){
            showAlert(@"删除成功");
            self.pageIndex = 1;
            [self getZhiWeiList:NO];
        }
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    
}

///点击列表上的完成
-(void)jobOkInfo:(NSString *)ID{
    
    
    
    NSString *url = [NSString stringWithFormat:@"%@?id=%@",Success_Job_Url,ID];
    [MSNetworkManager postRevertUrl:url params:@{} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess){
            showAlert(@"操作成功");
            self.pageIndex = 1;
            [self getZhiWeiList:NO];
        }
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    
}

-(void)getZhiWeiList:(BOOL)isMore{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:[NSString stringWithFormat:@"%ld",(long)self.selectIndex] forKey:@"type"];
    if (![NSString isNUllString:self.jobName]){
    [params setObject:self.jobName forKey:@"jobName"];
    }
    if (![NSString isNUllString:self.userName]) {
    [params setObject:self.userName forKey:@"userName"];
    }
    [params setObject:@"10" forKey:@"rows"];
    [params setObject:[NSString stringWithFormat:@"%ld",self.pageIndex] forKey:@"page"];
    
    
    
    [MSNetworkManager postRevertUrl:Job_List_Url params:params success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        if (isSuccess) {
            self.totalPage = [resonseObj[@"total"] intValue];
            NSArray *cacheArray = resonseObj[@"list"];
            if (isMore){
                if (cacheArray.count == 0){//没有更多数据
                    isMore?[self.tableView.mj_footer endRefreshing]:[self.tableView.mj_header endRefreshing];
                    return;
                } else {
                }
            } else {
                [self.dataAry removeAllObjects];
            }
           if (isMore == NO) {
               self.dataAry=[ZhiWeiGuanLiModel  mj_objectArrayWithKeyValuesArray:cacheArray];
               [self.tableView.mj_header endRefreshing];
           } else {
               NSMutableArray *cacheMoreArray = [ZhiWeiGuanLiModel  mj_objectArrayWithKeyValuesArray:cacheArray];
               [self.dataAry addObjectsFromArray:cacheMoreArray];
               [self.tableView.mj_footer endRefreshing];
           }
            if (self.dataAry.count > 0){
                self.tableView.backgroundColor = [UIColor colorWithHexString:@"#F9F9FA"];
            } else {
                self.tableView.backgroundColor = White_Color;
            }
           [self.tableView reloadData];
        }
        
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        
    } withShowHud:YES];
    

}

@end
